package com.lanou3g.dllo.baidumusic.musiclibrary.playlist;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.base.MyApp;
import com.lanou3g.dllo.baidumusic.tools.MyRvOnClickListener;
import com.lanou3g.dllo.baidumusic.tools.SingleVolley;

import java.util.List;

/**
 * Created by dllo on 16/6/21.
 */
public class PlaylistAdapter extends RecyclerView.Adapter<PlaylistAdapter.MyViewHolder> {

    private Context context;
    private PlaylistBean playlistBean;
    private List<PlaylistBean.ContentBean> list;
    private MyRvOnClickListener myRvOnClickListener;

    public PlaylistBean getPlaylistBean() {
        return playlistBean;
    }

    public void setMyRvOnClickListener(MyRvOnClickListener myRvOnClickListener) {
        this.myRvOnClickListener = myRvOnClickListener;
    }

    public PlaylistAdapter(Context context) {
        this.context = context;
    }

    public void setPlaylistBean(PlaylistBean playlistBean) {
        this.playlistBean = playlistBean;
        list = playlistBean.getContent();
        notifyDataSetChanged();
    }

    public void addPlaylistBean(PlaylistBean playlistBean){
        list.addAll(playlistBean.getContent());
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_playlist, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {

        PlaylistSingleQueue.getPlaylistSingleQueue(context).getImageLoader().get(list.get(position)
                .getPic_300(), ImageLoader.getImageListener(holder.playlistPhotoIv, R.mipmap.ic_launcher, R.mipmap.ic_launcher));
        holder.playlistSongsType.setText(list.get(position).getTitle());
        holder.playlistSongsComment.setText(list.get(position).getTag());
        holder.playlistListenedCountTv.setText(list.get(position).getListenum());
        if (myRvOnClickListener != null) {
            holder.playlistLayout.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // 当itemView被点击的时候
                    // getLayoutPosition是用来获得当前点击的position
                    int position = holder.getLayoutPosition();
                    // 调用接口对象的onClick方法
                    myRvOnClickListener.onClick(position);
                }
            });

        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        LinearLayout playlistLayout;
        ImageView playlistPhotoIv;
        TextView playlistSongsType, playlistSongsComment,playlistListenedCountTv;

        public MyViewHolder(View itemView) {
            super(itemView);

            playlistPhotoIv = (ImageView) itemView.findViewById(R.id.playlist_photo_iv);
            playlistSongsType = (TextView) itemView.findViewById(R.id.playlist_songs_type);
            playlistSongsComment = (TextView) itemView.findViewById(R.id.playlist_songs_comment);
            playlistLayout = (LinearLayout) itemView.findViewById(R.id.playlist_layout);
            playlistListenedCountTv = (TextView) itemView.findViewById(R.id.playlist_listenedcount_tv);

        }
    }
}
